package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.itheima.exception.BusinessRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
@Slf4j
public class ValidateCodeServiceImpl implements ValidateCodeService{

    String accessKey = "LTAI4GELVxSuvavBag31BBGb";
    String secret = "PcBZglzFzgXAItnaKyWCgCltfIidvS";
    String signName = "健康人生";
    String validateCodeTemplateCode = "SMS_162221957";
    String commonTemplateCode = "SMS_165106805";

    /**
     *  发送验证码短信 且存储验证码到redis
     * @param telephone  手机号码
     * @param type   发送短信的类型:预约，登录，获取密码
     * @param validateCode  要发送给用户的验证码
     */
    @Override
    public void sendValidateCodeShortMessage(String telephone, String type, String validateCode) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey, secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", telephone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", validateCodeTemplateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+validateCode+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.debug(response.getData());
            if(response.getHttpStatus() == 200){
                saveValidateCodeInRedis(telephone, type, validateCode);
                log.debug("发送短信成功！！");
            }else{
                log.debug("发送短信失败！！");
                throw new BusinessRuntimeException("发送验证码失败！！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("发送短信失败！！");
            throw new BusinessRuntimeException("发送验证码失败！！");
        }
    }

    @Autowired
    JedisPool jedisPool;

    private void saveValidateCodeInRedis(String telephone, String type, String validateCode){
        Jedis jedis = jedisPool.getResource();
        jedis.setex(type + "-" + telephone, 5 * 60 , validateCode);
    }

    /**
     * 校验验证码步骤：
     *  1. 获取redis中的验证码
     *  2. 如果验证码相同，返回true ，如果验证码不相同，返回false
     * @param telephone  手机号码
     * @param type  验证码的类型
     * @param validateCode  用户输入的验证码
     * @return
     */
    @Override
    public boolean checkValidateCode(String telephone, String type, String validateCode) {
        Jedis jedis = jedisPool.getResource();
        String validateCodeInRedis = jedis.get(type + "-" + telephone);
        //如果redis中验证码不为null， 且与用户输入的验证码一致，则校验成功，返回true
        if(validateCodeInRedis != null && validateCodeInRedis.equals(validateCode)){
            return true;
        }
        return false;
    }

    @Override
    public void sendCommonShortMessage(String telephone, String... param) {
        log.debug("此处应该发送通知短信");
    }
}
